package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.AddressService;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.math.BigDecimal;
import java.util.Date;
import java.util.FormatFlagsConversionMismatchException;
import java.util.List;

@Service
public class OderServiceImpl implements OrderService {


    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private CartService cartService;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Page<Order> findByPage(Integer pageNumber, Integer pageSize) {
        Page<Order> page = new Page<>(pageNumber, pageSize);
        LambdaQueryWrapper<Order> lq = new LambdaQueryWrapper<>();
        lq.eq(Order::getUserId, UserHolder.get().getId());
        page = orderMapper.selectPage(page, lq);
        if (page == null) {
            throw new CustomException("您还没有下蛋");
        } else {
            for (Order orders : page.getRecords()) {
                LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
                lqw.eq(OrderDetail::getOrderId, orders.getId());
                List<OrderDetail> orderDetails = orderDetailMapper.selectList(lqw);
                orders.setOrderDetails(orderDetails);
            }
            return page;
        }
    }

    @Override
    public void save(Order order) {

        //获取登陆人用户对象
        User user = UserHolder.get();
        if (user == null) {
            throw new CustomException("用户为空不能下单");
        }

        //根据id查询收货地址
        Address address = addressMapper.selectById(order.getAddressId());
        if (address == null) {
            throw new CustomException("地址为空请输入地址");
        }

        //获取购物车列表

        List<Cart> cart = cartService.findAll();

        if (CollectionUtil.isEmpty(cart)) {
            throw new CustomException("购物车不能为空");
        }

        //通过mp生成订单id
        long id = IdWorker.getId();
        //订单金额
        BigDecimal amount = new BigDecimal(0);

        //遍历购物购物车封装订单明细
        for (Cart cart1 : cart) {

            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(id); // 订单id
            BeanUtils.copyProperties(cart1, orderDetail);
//            orderDetail.setName(cart1.getName()); // 菜品或套餐名称
//            orderDetail.setDishId(cart1.getDishId()); // 菜品id
//            orderDetail.setSetmealId(cart1.getSetmealId());// 套餐id
//            orderDetail.setDishFlavor(cart1.getDishFlavor());// 口味
//            orderDetail.setNumber(cart1.getNumber()); // 数量
//            orderDetail.setAmount(cart1.getAmount()); // 金额
//            orderDetail.setImage(cart1.getImage()); // 图片
            // mapper保存
            orderDetailMapper.insert(orderDetail);

            // 累加
            // amount=amount+(cart.getAmount()*cart.getNumber());
            amount = amount.add(cart1.getAmount().multiply(new BigDecimal(cart1.getNumber())));
        }// 5.保存订单
        // 5-1 封装
        order.setId(id); // 订单id
        order.setNumber(String.valueOf(id)); // 订单号
        order.setStatus(1); // 待付款
        order.setUserId(user.getId()); // 下单用户id
        order.setOrderTime(new Date()); // 下单时间
        order.setCheckoutTime(new Date()); // 结账时间
        order.setAmount(amount); // 实收金额
        order.setUserName(user.getName()); // 下单用户名称
        order.setPhone(address.getPhone()); // 收货人手机号
        order.setAddress(address.getDetail()); // 收货人地址
        order.setConsignee(address.getConsignee()); // 收货人姓名
        // 5-2 mapper保存
        orderMapper.insert(order);

        // 6.清空购物车
        cartService.delete();
    }


    @Override
    public Page<Order> findByPage1(Integer pageNumber, Integer pageSize, Long number, String beginTime, String endTime) {
        Page<Order> page = new Page<>(pageNumber, pageSize);
        LambdaQueryWrapper<Order> lqw=new LambdaQueryWrapper<>();
        lqw.likeRight(null!=number,Order::getNumber,number).between(beginTime!=null||endTime!=null,Order::getOrderTime,beginTime,endTime);
        page=orderMapper.selectPage(page,lqw);
        return page;
    }
}
